package edu.response;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.file.Path;

@WebServlet("/downloadPicture")
public class DownloadPicture extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //response.setHeader("Content-Type","image/jpg; charset=utf-8");
        Path path = Path.of("D:\\TIM文件\\Java后端开发学习路线-高清版.jpg");
        response.setHeader("Content-disposition", "attachment;filename=" + path.getFileName() + ".jpg");
        ServletOutputStream outputStream = response.getOutputStream();
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\TIM文件\\Java后端开发学习路线-高清版.jpg"));
        
        int len;
        byte[] buffer = new byte[1024];
        while ((len = bis.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);
        }
        outputStream.flush();
        bis.close();
        outputStream.close();
    }
}
